package com.vipshop.compass.dao;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.vipshop.compass.common.GenericDaoHibernate;
import com.vipshop.compass.domain.Role;

@Repository("roleDao")
public class RoleDao extends GenericDaoHibernate<Role, Integer> {

	public RoleDao(Class<Role> persistentClass) {
		super(persistentClass);
	}

	public RoleDao() {
		super(Role.class);
	}

	public Role findByName(String name) {
		Query qry = getSession().createQuery("from Role r where r.name=:name");
		qry.setParameter("name", name);
		return (Role) qry.uniqueResult();
	}

	public boolean existsByName(String name) {
		Query qry = getSession().createQuery("select count(*) from Role r where r.name=:name");
		qry.setParameter("name", name);
		return ((Long) qry.uniqueResult()).intValue() > 0;
	}

}
